myApp
    .controller('cusSpendCtrl', function ($cookieStore,$scope, $http,CONFIG ,$window,$location) {

        /*根据user cookie 初始化加载菜单*/
        $scope.userName = $cookieStore.get("userName");

        $scope.saveCustomerCost = function (num, cost, score) {

            /*保存到DB*/
            var url = CONFIG.mainConfig.url+"/customerCost/add";

            $http({
                method: 'post',
                url: url,
                params: {
                    customerNum: num,
                    cost: cost,
                    score: score,
                    token: $cookieStore.get("token")
                }
            })
                .success(function (result) {

                    if (result.resultCode == 001) {
                        //TODO:
                        $scope.customer_num = "";
                        $scope.customer_cost = "";
                        $scope.customer_score = "";

                        //$window.location.href = "/html/cusSpend.html?num=1";
                    }
                    else if(result.resultCode == 002){
                        $scope.alertMsg = result.resultMsg;
                        alert(result.resultMsg);
                    }

                })
                .error(function () {
                    alert("error");
                });
        };

        function getRequest() {
            var url = location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for(var i = 0; i < strs.length; i ++) {
                    theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }

        //替换掉输入金额的时候的非法值
        $scope.costClearNoNum = function(attr){

            //先把非数字的都替换掉，除了数字和.
            attr = attr.replace(/[^\d.]/g,"");
            //必须保证第一个为数字而不是.
            attr = attr.replace(/^\./g,"");
            //保证只有出现一个.而没有多个.
            attr = attr.replace(/\.{2,}/g,"");
            //保证.只出现一次，而不能出现两次以上
            attr = attr.replace(".","$#$").replace(/\./g,"").replace("$#$",".");

            $scope.customer_cost = attr;
        };

        //替换掉输入积分的时候的非法值
        $scope.scoerClearNoNum = function(attr){

            //先把非数字的都替换掉，除了数字和.
            attr = attr.replace(/[^\d.]/g,"");
            //必须保证第一个为数字而不是.
            attr = attr.replace(/^\./g,"");
            //保证只有出现一个.而没有多个.
            attr = attr.replace(/\.{2,}/g,"");
            //保证.只出现一次，而不能出现两次以上
            attr = attr.replace(".","$#$").replace(/\./g,"").replace("$#$",".");

            $scope.customer_score = attr;
        };

        //如果从会员列表也点击会员名字连接到消费页面, 则预先填入会员号
        var param = getRequest();
        if(param.num!=undefined) {
            //document.getElementById("test").value=param.num;// = param.num;
            $scope.customer_num = param.num;
        }

    });